package com.meme.editor.property.value;

import org.apache.log4j.Logger;

import com.meme.editor.EntityObject;
import com.meme.editor.property.Property;
import com.meme.editor.property.PropertyValue;
import com.meme.editor.property.PropertyControl;
import com.meme.editor.property.control.InputPropertyControl;
import com.meme.editor.property.PropertyFactory;


import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

public class ItemsPropertyValue extends PropertyValue
{
	static Logger logger = Logger.getLogger (ItemsPropertyValue.class);
	
	List<Object> mSelectedItems = new LinkedList<Object> ();
	
	String mListNodePath;
	List<Object> mAllItems; // set by outer
	
	boolean mMultipalSelectionsAllowed;
	boolean mNullSelectionAllowed;
	
	public ItemsPropertyValue (Property property)
	{
		super (property);
		
		setMultipalSelectionsAllowed (false);
	}
	
	@Override
	public String getDefaultControlType ()
	{
		return PropertyFactory.ControlType_SelectList;
	}
	
	@Override
	public Object   getValue ()
	{
		return mSelectedItems;
	}
	
	@Override
	public String toString ()
	{
		String ret = "";
		for (Object o : mSelectedItems)
			ret = ret + ";" + o.toString();
		
		return ret;
	}
	
	@Override
	protected boolean modify (Object newValue)
	{
		creatAllItems ();
		
		if ( newValue instanceof List )
		{
			if (newValue == mSelectedItems)
				return true;
			
			mSelectedItems.clear();
			
			List<Object> list = (List<Object>) newValue;
			for (Object s : list)
			{
				for (Object o : mAllItems)
				{
					if (o.toString().equals(s.toString()))
						mSelectedItems.add (o);
				}
			}
			
			return true;
		}
		else if( newValue instanceof String )
		{
			mSelectedItems.clear();
			
			
			String[] strings = ((String)(newValue)).split(";");
			for (String s : strings)
			{
				for (Object o : mAllItems)
				{
					if (o.toString().equals(s))
						mSelectedItems.add (o);
				}
			}
			
			return true;
		}
		
		mSelectedItems.clear();
		
		return false;
	}
	
	@Override
	public String getValueAsString ()
	{
		String str = "";
		for (Object o : mSelectedItems)
			str += o.toString() + ";";
		
		return str;
	}

	@Override
	protected Object   makeLegalValueFromControl (Object[] params)
	{
		creatAllItems ();
		
		if (getControlType ().equals (PropertyFactory.ControlType_SelectList) )
		{
			int selectIndex = ( (Integer) params[0] ).intValue();
			if ( selectIndex <= 0 )
				return null;
			else
			{
				return mAllItems.get (selectIndex - 1).toString();
			}
		}
		else
			return null;
	}
	
	@Override
	protected Object[] makeDataForControl ()
	{
		// now only ControlType_SelectList is supported
		
		
		Vector list = new Vector ();
		list.add("");
		creatAllItems ();
			
		if ( mAllItems != null )
		{
			list.addAll (mAllItems);
		}

		// to do ...
		
		int index = 0;
		if (mSelectedItems.size() > 0)
		{
			index = mAllItems.indexOf(mSelectedItems.get(0)) + 1;
		}

		
		Object[] params = new Object[]
		      {
				index,
				list
		      };
		
		return params;
	}
	
	void creatAllItems ()
	{
		if ( mAllItems == null )
		{
			try
			{
				EntityObject entity = (EntityObject) ( getProperty ().getOwner () );
				mAllItems = (List<Object>) entity.parseNodePath(mListNodePath);
			}
			catch (Exception e)
			{
				logger.error ("Error when parsing mListNodePath.");
			}
		}
	}
	
	public final static String Param_ItemList = "item_list";
	
	@Override
	protected boolean _setParameter (String name, Object value)
	{
		// defaultValue
		// canSelectNothing
		// canSelectMultiItems
		// listNodePath or listName
		
		if ( name.equals(Param_ItemList) )
		{
			mListNodePath = (String) value;
			
			return true;
		}
		
		return false;
	}
	
	public boolean isMultipalSelectionsAllowed ()
	{
		return mMultipalSelectionsAllowed;
	}
	
	public void setMultipalSelectionsAllowed (boolean multiItemsSelectable)
	{
		mMultipalSelectionsAllowed = multiItemsSelectable;
	}
	
	public int getSelectedIndex ()
	{
		if (mSelectedItems.size() == 0)
			return -1;
		
		creatAllItems ();
		
		Object item = mSelectedItems.get(0);
		return mAllItems.indexOf(item);
	}
	
	public int[] getSelectedIndices ()
	{
		int[] indices = new int[mSelectedItems.size()];

		creatAllItems ();
		int index = 0;
		for (Object item : mSelectedItems)
		{
			indices[index ++] = mAllItems.indexOf(item);
		}
		
		return indices;
	}
}
